
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Troubleshooting · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Glossary.html" />
    
    
    <link rel="prev" href="faq/Miscellaneous.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Read Me
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="introduction/">
            
                <a href="introduction/">
            
                    
                    Introduction
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="introduction/Motivation.html">
            
                <a href="introduction/Motivation.html">
            
                    
                    Motivation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="introduction/CoreConcepts.html">
            
                <a href="introduction/CoreConcepts.html">
            
                    
                    Core Concepts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="introduction/ThreePrinciples.html">
            
                <a href="introduction/ThreePrinciples.html">
            
                    
                    Three Principles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="introduction/PriorArt.html">
            
                <a href="introduction/PriorArt.html">
            
                    
                    Prior Art
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="introduction/Ecosystem.html">
            
                <a href="introduction/Ecosystem.html">
            
                    
                    Ecosystem
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="introduction/Examples.html">
            
                <a href="introduction/Examples.html">
            
                    
                    Examples
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="basics/">
            
                <a href="basics/">
            
                    
                    Basics
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="basics/Actions.html">
            
                <a href="basics/Actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="basics/Reducers.html">
            
                <a href="basics/Reducers.html">
            
                    
                    Reducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="basics/Store.html">
            
                <a href="basics/Store.html">
            
                    
                    Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="basics/DataFlow.html">
            
                <a href="basics/DataFlow.html">
            
                    
                    Data Flow
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="basics/UsageWithReact.html">
            
                <a href="basics/UsageWithReact.html">
            
                    
                    Usage with React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="basics/ExampleTodoList.html">
            
                <a href="basics/ExampleTodoList.html">
            
                    
                    Example: Todo List
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="advanced/">
            
                <a href="advanced/">
            
                    
                    Advanced
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="advanced/AsyncActions.html">
            
                <a href="advanced/AsyncActions.html">
            
                    
                    Async Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="advanced/AsyncFlow.html">
            
                <a href="advanced/AsyncFlow.html">
            
                    
                    Async Flow
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="advanced/Middleware.html">
            
                <a href="advanced/Middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="advanced/UsageWithReactRouter.html">
            
                <a href="advanced/UsageWithReactRouter.html">
            
                    
                    Usage with React Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="advanced/ExampleRedditAPI.html">
            
                <a href="advanced/ExampleRedditAPI.html">
            
                    
                    Example: Reddit API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" >
            
                <span>
            
                    
                    Next Steps
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="recipes/">
            
                <a href="recipes/">
            
                    
                    Recipes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="recipes/MigratingToRedux.html">
            
                <a href="recipes/MigratingToRedux.html">
            
                    
                    Migrating to Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="recipes/UsingObjectSpreadOperator.html">
            
                <a href="recipes/UsingObjectSpreadOperator.html">
            
                    
                    Using Object Spread Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="recipes/ReducingBoilerplate.html">
            
                <a href="recipes/ReducingBoilerplate.html">
            
                    
                    Reducing Boilerplate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="recipes/ServerRendering.html">
            
                <a href="recipes/ServerRendering.html">
            
                    
                    Server Rendering
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="recipes/WritingTests.html">
            
                <a href="recipes/WritingTests.html">
            
                    
                    Writing Tests
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="recipes/ComputingDerivedData.html">
            
                <a href="recipes/ComputingDerivedData.html">
            
                    
                    Computing Derived Data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="recipes/ImplementingUndoHistory.html">
            
                <a href="recipes/ImplementingUndoHistory.html">
            
                    
                    Implementing Undo History
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="recipes/IsolatingSubapps.html">
            
                <a href="recipes/IsolatingSubapps.html">
            
                    
                    Isolating Subapps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="recipes/StructuringReducers.html">
            
                <a href="recipes/StructuringReducers.html">
            
                    
                    Structuring Reducers
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.9.1" data-path="recipes/reducers/PrerequisiteConcepts.html">
            
                <a href="recipes/reducers/PrerequisiteConcepts.html">
            
                    
                    Prerequisite Concepts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.2" data-path="recipes/reducers/BasicReducerStructure.html">
            
                <a href="recipes/reducers/BasicReducerStructure.html">
            
                    
                    Basic Reducer Structure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.3" data-path="recipes/reducers/SplittingReducerLogic.html">
            
                <a href="recipes/reducers/SplittingReducerLogic.html">
            
                    
                    Splitting Reducer Logic
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.4" data-path="recipes/reducers/RefactoringReducersExample.html">
            
                <a href="recipes/reducers/RefactoringReducersExample.html">
            
                    
                    Refactoring Reducers Example
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.5" data-path="recipes/reducers/UsingCombineReducers.html">
            
                <a href="recipes/reducers/UsingCombineReducers.html">
            
                    
                    Using combineReducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.6" data-path="recipes/reducers/BeyondCombineReducers.html">
            
                <a href="recipes/reducers/BeyondCombineReducers.html">
            
                    
                    Beyond combineReducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.7" data-path="recipes/reducers/NormalizingStateShape.html">
            
                <a href="recipes/reducers/NormalizingStateShape.html">
            
                    
                    Normalizing State Shape
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.8" data-path="recipes/reducers/UpdatingNormalizedData.html">
            
                <a href="recipes/reducers/UpdatingNormalizedData.html">
            
                    
                    Updating Normalized Data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.9" data-path="recipes/reducers/ReusingReducerLogic.html">
            
                <a href="recipes/reducers/ReusingReducerLogic.html">
            
                    
                    Reusing Reducer Logic
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.10" data-path="recipes/reducers/ImmutableUpdatePatterns.html">
            
                <a href="recipes/reducers/ImmutableUpdatePatterns.html">
            
                    
                    Immutable Update Patterns
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.11" data-path="recipes/reducers/InitializingState.html">
            
                <a href="recipes/reducers/InitializingState.html">
            
                    
                    Initializing State
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="recipes/UsingImmutableJS.html">
            
                <a href="recipes/UsingImmutableJS.html">
            
                    
                    Using Immutable.JS with Redux
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="FAQ.html">
            
                <a href="FAQ.html">
            
                    
                    FAQ
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="faq/General.html">
            
                <a href="faq/General.html">
            
                    
                    General
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="faq/Reducers.html">
            
                <a href="faq/Reducers.html">
            
                    
                    Reducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="faq/OrganizingState.html">
            
                <a href="faq/OrganizingState.html">
            
                    
                    Organizing State
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="faq/StoreSetup.html">
            
                <a href="faq/StoreSetup.html">
            
                    
                    Store Setup
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="faq/Actions.html">
            
                <a href="faq/Actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="faq/ImmutableData.html">
            
                <a href="faq/ImmutableData.html">
            
                    
                    Immutable Data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="faq/CodeStructure.html">
            
                <a href="faq/CodeStructure.html">
            
                    
                    Code Structure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="faq/Performance.html">
            
                <a href="faq/Performance.html">
            
                    
                    Performance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="faq/ReactRedux.html">
            
                <a href="faq/ReactRedux.html">
            
                    
                    React Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="faq/Miscellaneous.html">
            
                <a href="faq/Miscellaneous.html">
            
                    
                    Miscellaneous
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="Troubleshooting.html">
            
                <a href="Troubleshooting.html">
            
                    
                    Troubleshooting
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="Glossary.html">
            
                <a href="Glossary.html">
            
                    
                    Glossary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="api/">
            
                <a href="api/">
            
                    
                    API Reference
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="api/createStore.html">
            
                <a href="api/createStore.html">
            
                    
                    createStore
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="api/Store.html">
            
                <a href="api/Store.html">
            
                    
                    Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="api/combineReducers.html">
            
                <a href="api/combineReducers.html">
            
                    
                    combineReducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="api/applyMiddleware.html">
            
                <a href="api/applyMiddleware.html">
            
                    
                    applyMiddleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="api/bindActionCreators.html">
            
                <a href="api/bindActionCreators.html">
            
                    
                    bindActionCreators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="api/compose.html">
            
                <a href="api/compose.html">
            
                    
                    compose
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="CHANGELOG.md">
            
                <span>
            
                    
                    Change Log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="PATRONS.md">
            
                <span>
            
                    
                    Patrons
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="Feedback.html">
            
                <a href="Feedback.html">
            
                    
                    Feedback
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Troubleshooting</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="troubleshooting">Troubleshooting</h1>
<p>This is a place to share common problems and solutions to them.<br>The examples use React, but you should still find them useful if you use something else.</p>
<h3 id="nothing-happens-when-i-dispatch-an-action">Nothing happens when I dispatch an action</h3>
<p>Sometimes, you are trying to dispatch an <a href="Glossary.html#action" class="glossary-term" title="">action</a>, but your view does not update. Why does this happen? There may be several reasons for this.</p>
<h4 id="never-mutate-reducer-arguments">Never mutate reducer arguments</h4>
<p>It is tempting to modify the <code>state</code> or <code>action</code> passed to you by Redux. Don&apos;t do this!</p>
<p>Redux assumes that you never mutate the objects it gives to you in the <a href="Glossary.html#reducer" class="glossary-term" title="">reducer</a>. <strong>Every single time, you must return the new <a href="Glossary.html#state" class="glossary-term" title="">state</a> object.</strong> Even if you don&apos;t use a library like <a href="https://facebook.github.io/immutable-js/" target="_blank">Immutable</a>, you need to completely avoid mutation.</p>
<p>Immutability is what lets <a href="https://github.com/gaearon/react-redux" target="_blank">react-redux</a> efficiently subscribe to fine-grained updates of your <a href="Glossary.html#state" class="glossary-term" title="">state</a>. It also enables great developer experience features such as time travel with <a href="http://github.com/gaearon/redux-devtools" target="_blank">redux-devtools</a>.</p>
<p>For example, a <a href="Glossary.html#reducer" class="glossary-term" title="">reducer</a> like this is wrong because it mutates the <a href="Glossary.html#state" class="glossary-term" title="">state</a>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">todos</span>(<span class="hljs-params">state = [], action</span>) </span>{
  <span class="hljs-keyword">switch</span> (action.type) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;ADD_TODO&apos;</span>:
      <span class="hljs-comment">// Wrong! This mutates state</span>
      state.push({
        text: action.text,
        completed: <span class="hljs-literal">false</span>
      })
      <span class="hljs-keyword">return</span> state
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;COMPLETE_TODO&apos;</span>:
      <span class="hljs-comment">// Wrong! This mutates state[action.index].</span>
      state[action.index].completed = <span class="hljs-literal">true</span>
      <span class="hljs-keyword">return</span> state
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">return</span> state
  }
}
</code></pre>
<p>It needs to be rewritten like this:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">todos</span>(<span class="hljs-params">state = [], action</span>) </span>{
  <span class="hljs-keyword">switch</span> (action.type) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;ADD_TODO&apos;</span>:
      <span class="hljs-comment">// Return a new array</span>
      <span class="hljs-keyword">return</span> [
        ...state,
        {
          text: action.text,
          completed: <span class="hljs-literal">false</span>
        }
      ]
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;COMPLETE_TODO&apos;</span>:
      <span class="hljs-comment">// Return a new array</span>
      <span class="hljs-keyword">return</span> state.map((todo, index) =&gt; {
        <span class="hljs-keyword">if</span> (index === action.index) {
          <span class="hljs-comment">// Copy the object before mutating</span>
          <span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign({}, todo, {
            completed: <span class="hljs-literal">true</span>
          })
        }
        <span class="hljs-keyword">return</span> todo
      })
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">return</span> state
  }
}
</code></pre>
<p>It&apos;s more code, but it&apos;s exactly what makes Redux predictable and efficient. If you want to have less code, you can use a helper like <a href="https://facebook.github.io/react/docs/update.html" target="_blank"><code>React.addons.update</code></a> to write immutable transformations with a terse syntax:</p>
<pre><code class="lang-js"><span class="hljs-comment">// Before:</span>
<span class="hljs-keyword">return</span> state.map((todo, index) =&gt; {
  <span class="hljs-keyword">if</span> (index === action.index) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign({}, todo, {
      completed: <span class="hljs-literal">true</span>
    })
  }
  <span class="hljs-keyword">return</span> todo
})

<span class="hljs-comment">// After</span>
<span class="hljs-keyword">return</span> update(state, {
  [action.index]: {
    completed: {
      $set: <span class="hljs-literal">true</span>
    }
  }
})
</code></pre>
<p>Finally, to update objects, you&apos;ll need something like <code>_.extend</code> from Underscore, or better, an <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign" target="_blank"><code>Object.assign</code></a> polyfill.</p>
<p>Make sure that you use <code>Object.assign</code> correctly. For example, instead of returning something like <code>Object.assign(state, newData)</code> from your reducers, return <code>Object.assign({}, state, newData)</code>. This way you don&apos;t override the previous <code>state</code>.</p>
<p>You can also enable the <a href="recipes/UsingObjectSpreadOperator.html">object spread operator proposal</a> for a more succinct syntax:</p>
<pre><code class="lang-js"><span class="hljs-comment">// Before:</span>
<span class="hljs-keyword">return</span> state.map((todo, index) =&gt; {
  <span class="hljs-keyword">if</span> (index === action.index) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign({}, todo, {
      completed: <span class="hljs-literal">true</span>
    })
  }
  <span class="hljs-keyword">return</span> todo
})

<span class="hljs-comment">// After:</span>
<span class="hljs-keyword">return</span> state.map((todo, index) =&gt; {
  <span class="hljs-keyword">if</span> (index === action.index) {
    <span class="hljs-keyword">return</span> { ...todo, completed: <span class="hljs-literal">true</span> }
  }
  <span class="hljs-keyword">return</span> todo
})
</code></pre>
<p>Note that experimental language features are subject to change.</p>
<p>Also keep an eye out for nested <a href="Glossary.html#state" class="glossary-term" title="">state</a> objects that need to be deeply copied. Both <code>_.extend</code> and <code>Object.assign</code> make a shallow copy of the <a href="Glossary.html#state" class="glossary-term" title="">state</a>. See <a href="docs/recipes/reducers/ImmutableUpdatePatterns.md#updating-nested-objects">Updating Nested Objects</a> for suggestions on how to deal with nested <a href="Glossary.html#state" class="glossary-term" title="">state</a> objects.</p>
<h4 id="dont-forget-to-call-dispatchaction">Don&apos;t forget to call <a href="api/Store.html#dispatch"><code>dispatch(action)</code></a></h4>
<p>If you define an <a href="Glossary.html#action" class="glossary-term" title="">action</a> creator, calling it will <em>not</em> automatically dispatch the <a href="Glossary.html#action" class="glossary-term" title="">action</a>. For example, this code will do nothing:</p>
<h4 id="todoactionsjs"><code>TodoActions.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addTodo</span>(<span class="hljs-params">text</span>) </span>{
  <span class="hljs-keyword">return</span> { type: <span class="hljs-string">&apos;ADD_TODO&apos;</span>, text }
}
</code></pre>
<h4 id="addtodojs"><code>AddTodo.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> { addTodo } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./TodoActions&apos;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddTodo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  handleClick() {
    <span class="hljs-comment">// Won&apos;t work!</span>
    addTodo(<span class="hljs-string">&apos;Fix the issue&apos;</span>)
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> this.handleClick()}&gt;
        Add
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
    )
  }
}
</code></pre>
<p>It doesn&apos;t work because your <a href="Glossary.html#action" class="glossary-term" title="">action</a> creator is just a function that <em>returns</em> an <a href="Glossary.html#action" class="glossary-term" title="">action</a>. It is up to you to actually dispatch it. We can&apos;t bind your <a href="Glossary.html#action" class="glossary-term" title="">action</a> creators to a particular <a href="Glossary.html#store" class="glossary-term" title="">Store</a> instance during the definition because apps that render on the server need a separate Redux <a href="Glossary.html#store" class="glossary-term" title="">store</a> for every request.</p>
<p>The fix is to call <a href="api/Store.html#dispatch"><code>dispatch()</code></a> method on the <a href="api/Store.html">store</a> instance:</p>
<pre><code class="lang-js">handleClick() {
  <span class="hljs-comment">// Works! (but you need to grab store somehow)</span>
  store.dispatch(addTodo(<span class="hljs-string">&apos;Fix the issue&apos;</span>))
}
</code></pre>
<p>If you&apos;re somewhere deep in the component hierarchy, it is cumbersome to pass the <a href="Glossary.html#store" class="glossary-term" title="">store</a> down manually. This is why <a href="https://github.com/gaearon/react-redux" target="_blank">react-redux</a> lets you use a <code>connect</code> <a href="https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750" target="_blank">higher-order component</a> that will, apart from subscribing you to a Redux <a href="Glossary.html#store" class="glossary-term" title="">store</a>, inject <code>dispatch</code> into your component&apos;s props.</p>
<p>The fixed code looks like this:</p>
<h4 id="addtodojs"><code>AddTodo.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>
<span class="hljs-keyword">import</span> { addTodo } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./TodoActions&apos;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddTodo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  handleClick() {
    <span class="hljs-comment">// Works!</span>
    <span class="hljs-keyword">this</span>.props.dispatch(addTodo(<span class="hljs-string">&apos;Fix the issue&apos;</span>))
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> this.handleClick()}&gt;
        Add
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
    )
  }
}

<span class="hljs-comment">// In addition to the state, `connect` puts `dispatch` in our props.</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect()(AddTodo)
</code></pre>
<p>You can then pass <code>dispatch</code> down to other components manually, if you want to.</p>
<h4 id="make-sure-mapstatetoprops-is-correct">Make sure mapStateToProps is correct</h4>
<p>It&apos;s possible you&apos;re correctly dispatching an <a href="Glossary.html#action" class="glossary-term" title="">action</a> and applying your <a href="Glossary.html#reducer" class="glossary-term" title="">reducer</a> but the corresponding <a href="Glossary.html#state" class="glossary-term" title="">state</a> is not being correctly translated into props.</p>
<h2 id="something-else-doesnt-work">Something else doesn&apos;t work</h2>
<p>Ask around on the <strong>#redux</strong> <a href="http://reactiflux.com/" target="_blank">Reactiflux</a> Discord channel, or <a href="https://github.com/reactjs/redux/issues" target="_blank">create an issue</a>.<br>If you figure it out, <a href="https://github.com/reactjs/redux/edit/master/docs/Troubleshooting.md" target="_blank">edit this document</a> as a courtesy to the next person having the same problem.</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="faq/Miscellaneous.html" class="navigation navigation-prev " aria-label="Previous page: Miscellaneous">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Glossary.html" class="navigation navigation-next " aria-label="Next page: Glossary">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Troubleshooting","level":"1.7","depth":1,"next":{"title":"Glossary","level":"1.8","depth":1,"path":"Glossary.md","ref":"./Glossary.md","articles":[]},"previous":{"title":"Miscellaneous","level":"1.6.10","depth":2,"path":"faq/Miscellaneous.md","ref":"./faq/Miscellaneous.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Troubleshooting.md","mtime":"2017-03-13T14:28:11.282Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-15T14:47:19.389Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

